﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace GameDataLibrary
{

    public class ScriptContent
    {
        private string scriptLocation;
        public string ScriptLocation
        {
            get { return scriptLocation; }
            set { scriptLocation = value; }
        }

        /// <summary>
        /// 文字脚本
        /// </summary>
        private string scriptsName;
        public string ScriptsName
        {
            get { return scriptsName; }
            set { scriptsName = value; }
        }

        private string fontTypeLocation;
        public string FontTypeLocation
        {
            get { return fontTypeLocation; }
            set { fontTypeLocation = value; }
        }

        private string scriptWindowLocation;
        public string ScriptWindowLocation
        {
            get { return scriptWindowLocation; }
            set { scriptWindowLocation = value; }
        }

        private string bgsPicLocation;
        public string BgsPicLocation
        {
            get { return bgsPicLocation; }
            set { bgsPicLocation = value; }
        }



        /// <summary>
        /// 保存背景图片名字的集合
        /// </summary>
        private string bgPicsName;        
        public string BgPicsName
        {
            get { return bgPicsName; }
            set { bgPicsName = value; }
        }

        private string songsLocation;
        public string SongsLocation
        {
            get { return songsLocation; }
            set { songsLocation = value; }
        }

        /// <summary>
        /// 该章节的背景音乐名字的集合
        /// </summary>
        private string songsName;
        public string SongsName
        {
            get { return songsName; }
            set { songsName = value; }
        }



        #region 非标准数据
        private string[] scripts;
        [ContentSerializerIgnore]
        public string[] Scripts
        {
            get { return scripts; }
            set { scripts = value; }
        }

        private SpriteFont fontType;
        [ContentSerializerIgnore]
        public SpriteFont FontType
        {
            get { return fontType; }
            set { fontType = value; }
        }

        private Texture2D scriptWindow;
        [ContentSerializerIgnore]
        public Texture2D ScriptWindow
        {
            get { return scriptWindow; }
            set { scriptWindow = value; }
        }

        private Dictionary<string, Song> songs;
        [ContentSerializerIgnore]
        public Dictionary<string,Song> Songs
        {
            get { return songs; }
            set { songs = value; }
        }

        private Dictionary<string, Texture2D> bgPictures;
        [ContentSerializerIgnore]
        public Dictionary<string, Texture2D> BgPictures
        {
            get { return bgPictures; }
            set { bgPictures = value; }
        }

        #endregion

    }
    public class ScriptContentReader : ContentTypeReader<ScriptContent>
    {
        protected override ScriptContent Read(ContentReader input, ScriptContent scriptContext)
        {
            if (scriptContext == null)
            {
                scriptContext = new ScriptContent();
            }

            //加载XML里的记录
            scriptContext.ScriptLocation = input.ReadString();
            scriptContext.ScriptsName = input.ReadString();
            scriptContext.FontTypeLocation = input.ReadString();
            scriptContext.BgsPicLocation = input.ReadString();
            scriptContext.BgPicsName = input.ReadString();
            scriptContext.SongsLocation = input.ReadString();
            scriptContext.SongsName = input.ReadString();
            scriptContext.ScriptWindowLocation = input.ReadString();


            //自定义数据加载
            scriptContext.BgPictures = LoadPictures(input, scriptContext, scriptContext.BgsPicLocation);
            scriptContext.Songs = LoadSongs(input, scriptContext, scriptContext.SongsLocation);
            scriptContext.FontType = input.ContentManager.Load<SpriteFont>(scriptContext.FontTypeLocation); 
            scriptContext.ScriptWindow = input.ContentManager.Load<Texture2D>(scriptContext.ScriptWindowLocation);


            return scriptContext;
        }

        private static Dictionary<string, Texture2D> LoadPictures(ContentReader input, ScriptContent scriptContext, string fileLocation)
        {
            Dictionary<string, Texture2D> pics = new Dictionary<string, Texture2D>();
            string[] strBackGrounds = scriptContext.BgPicsName.Split(';');
            foreach (string str in strBackGrounds)
            {
                pics.Add(str, input.ContentManager.Load<Texture2D>(fileLocation + str));
            }
            return pics;
        }

        /// <summary>
        /// 加载歌曲集合
        /// </summary>
        /// <param name="input">序列化读取对象</param>
        /// <param name="scriptContext">加载的对象类</param>
        /// <returns></returns>
        private static Dictionary<string, Song> LoadSongs(ContentReader input, ScriptContent scriptContext, string songsLocation)
        {
            string[] strBackGrounds = scriptContext.SongsName.Split(';');
            Dictionary<string, Song> songs = new Dictionary<string, Song>();
            foreach (string str in strBackGrounds)
            {
                songs.Add(str, input.ContentManager.Load<Song>(songsLocation + str));
            }
            return songs;
        }
    }
}
